# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from Publisher import models


class Bookadmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'ALL_authors', 'publisher', 'publish_date', 'publish_status')
    search_fields = ('name',)
    list_filter = ('publisher', 'publish_date')
    list_per_page = 3              # 每页显示项数目
    list_display_links = ('id', )  # 可以链接到具体项的页面
    list_editable = ('name', 'publish_status')      # 可编辑字段
    filter_horizontal = ('authors',)      # 水平界面的多选框
    date_hierarchy = 'publish_date'       # 更改列表将包含一个依据这个字段基于日期的下拉导航
    list_select_related = ('publisher',)  # 在检索管理更改列表页面上的对象列表时使用select_related()
    raw_id_fields = ('publisher',)  # 窗口选择
    actions = ["set_publish_checkout", "set_publish_wait", "set_publish_checking"]

    # 设置出版状态
    def set_publish_checkout(modeladmin, request, queryset):
        selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
        models.Book.objects.filter(id__in=selected).update(publish_status='checkout')

    def set_publish_wait(modeladmin, request, queryset):
        selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
        models.Book.objects.filter(id__in=selected).update(publish_status='wait')

    def set_publish_checking(modeladmin, request, queryset):
        selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
        models.Book.objects.filter(id__in=selected).update(publish_status='checking')

    # 设置Action选项用中文显示
    set_publish_checkout.short_description = u"设置所选书籍状态为---已出版"
    set_publish_wait.short_description = u"设置所选书籍状态为---待出版"
    set_publish_checking.short_description = u"设置所选书籍状态为---审核中"


class Authoradmin(admin.ModelAdmin):
    list_display = ('last_name', 'first_name', 'email')


class Publisheradmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'address', 'city', 'state_province')


# Register your models here.
admin.site.register(models.Author, Authoradmin)
admin.site.register(models.Publisher, Publisheradmin)
admin.site.register(models.Book, Bookadmin)
